package com.yjf.ljr.activity;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.yjf.ljr.R;
import com.yjf.ljr.activity.IjActivity.ID;
import com.yjf.ljr.adapter.CommonAdapter;
import com.yjf.ljr.adapter.CommonAdapter.HandleCallBack;
import com.yjf.ljr.common.CommonData;
import com.yjf.ljr.dataclass.CanTransListDataClass;
import com.yjf.ljr.dataclass.CancelTransferDataClass;
import com.yjf.ljr.dataclass.TransferDebtDataClass;
import com.yjf.ljr.dataclass.CanTransListDataClass.CanTransListInfo;
import com.yjf.ljr.dataclass.TransferDebtDataClass.TransfersListInfo;
import com.yjf.ljr.parser.RequestBuilder.RequestObject;
import com.yjf.ljr.util.AppUtil;
import com.yjf.ljr.view.DialogView;
import com.yjf.ljr.view.XListView;
import com.yjf.ljr.view.XListView.IXListViewListener;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MyDebtorActivity extends IjActivity implements OnClickListener {
	@ID("vpDebtor")
	private ViewPager mVpDebtor;
	private List<View> mListView;// view 集合
	@ID("ivCursorDebtor")
	private ImageView mIvCursorDebtor;
	private int mBmpW;// 动画图片宽度
	private int mOffset = 0;// 动画图片偏移量
	private XListView mXvTransferable;// 可转让
	private XListView mXvTransferring;// 转让中
	private XListView mXvTransferablealready;// 已转让
	private XListView mXvAlreadyBuy;// 已购买
	private CommonAdapter mCaTransferable;// 可转让
	private CommonAdapter mCaTransferring;// 转让中
	private CommonAdapter mCaTransferablealready;// 已转让
	private CommonAdapter mCaAlreadyBuy;// 已购买

	@ID("tvTransferable")
	private TextView mTvTransferable;// 可转让
	@ID("tvTransferring")
	private TextView mTvTransferring;// 转让中
	@ID("tvTransferablealready")
	private TextView mtvTransferablealready;// 已转让
	@ID("tvAlreadyBuy")
	private TextView mTvAlreadyBuy;// 已购买
	private List<CanTransListInfo> mListTransferaable;// 可转让列表
	private List<TransfersListInfo> mListtransferablealready;// 已转让列表
	private List<TransfersListInfo> mListTransfering;// 转让中列表
	private List<TransfersListInfo> mListAleradyBuy;// 已购买列表
	private int currIndex = 0;// 当前页卡编号
	private int mTabCurrentPosition = 0;
	private DialogView mDialogTransfering;// 转让取消弹框
	private View transferingView;// 转让取消弹框view
	private Button mBtDialogtrasferOk;//
	private Button mBtDialogtrasferCancel;//
	private int pageNumber = 1;
	private int pageSize = 10;
	private static final String REQUESTMETHOD = "canTransList";
	private String statusRefresh;
	private String investAmount;// 投资金额

	private String tradeIdDl;
	private String tradeTransferIdDl;
	DecimalFormat df = new DecimalFormat("######0.00");
	private static String statusRevoke;// 撤销状态
	// @ID("tv_title_right")
	// private TextView mTvtv_title_right;// 标题右边

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_mydebtor);
		initControl();
		initView();
		InitImageView();
	}

	private void initControl() {
		setTitleStr("我的债权");
		setLeftBtnClick();
		// mTvtv_title_right.setText("已购买");
		// mTvtv_title_right.setOnClickListener(this);
		mListView = new ArrayList<View>();
		mListTransferaable = new ArrayList<CanTransListInfo>();// 可转列表
		mListTransfering = new ArrayList<TransfersListInfo>();// 转让中列表
		mListtransferablealready = new ArrayList<TransfersListInfo>();// 已转让列表
		mListAleradyBuy = new ArrayList<TransfersListInfo>();// 已购买列表
		transferingView = LayoutInflater.from(mContext).inflate(R.layout.dialog_transfering, null);
		mBtDialogtrasferOk = (Button) transferingView.findViewById(R.id.btDialogtrasferOk);
		mBtDialogtrasferCancel = (Button) transferingView.findViewById(R.id.btDialogtrasferCancel);
		mBtDialogtrasferCancel.setOnClickListener(this);
		mBtDialogtrasferOk.setOnClickListener(this);
		mDialogTransfering = new DialogView(mContext, transferingView);
		mTvTransferable.setSelected(true);// 可转让
		mTvTransferring.setSelected(false);// 转让中
		mtvTransferablealready.setSelected(false);// 已转让
		mTvAlreadyBuy.setSelected(false);// 已购买
		mTvTransferable.setOnClickListener(this);// 可转让
		mTvTransferring.setOnClickListener(this);// 转让中
		mtvTransferablealready.setOnClickListener(this);// 已转让
		mTvAlreadyBuy.setOnClickListener(this);// 已购买
		showProgressDialog();
		new TransfersListTask(pageNumber, pageSize, false, REQUESTMETHOD, "").execute();
	}

	@SuppressLint("SimpleDateFormat")
	private void initView() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		LayoutInflater mInflater = getLayoutInflater();
		// 可转让
		View viewTransferable = mInflater.inflate(R.layout.transferable_lisstview, null);
		mListView.add(viewTransferable);
		mXvTransferable = (XListView) viewTransferable.findViewById(R.id.xlvTransferable);
		mCaTransferable = new CommonAdapter(mContext, mListTransferaable, R.layout.item_transferaable,
				ViewHolderTransferable.class, handleCallBackTransferable);
		mXvTransferable.setAdapter(mCaTransferable);

		mXvTransferable.setPullLoadEnable(true);
		mXvTransferable.setPullRefreshEnable(true);
		mXvTransferable.mFooterView.hide(); // 隐藏更多
		mXvTransferable.setRefreshTime(sdf.format(new Date()));
		mXvTransferable.setXListViewListener(new IXListViewListener() {
			@Override
			public void onRefresh() {
				pageNumber = 1;
				new TransfersListTask(pageNumber, pageSize, false, REQUESTMETHOD, "").execute();
			}

			@Override
			public void onLoadMore() {
				pageNumber++;
				new TransfersListTask(pageNumber, pageSize, true, REQUESTMETHOD, "").execute();
			}
		});

		// 转让中
		View viewTransferableing = mInflater.inflate(R.layout.transferring_lisstview, null);
		mListView.add(viewTransferableing);
		mXvTransferring = (XListView) viewTransferableing.findViewById(R.id.xlvTransferring);
		mCaTransferring = new CommonAdapter(mContext, mListTransfering, R.layout.item_transfering,
				ViewHolderTransfering.class, handleCallBackTransfering);
		mXvTransferring.setAdapter(mCaTransferring);
		mXvTransferring.setPullLoadEnable(true);
		mXvTransferring.setPullRefreshEnable(true);
		mXvTransferring.mFooterView.hide(); // 隐藏更多
		mXvTransferring.setRefreshTime(sdf.format(new Date()));
		mXvTransferring.setXListViewListener(new IXListViewListener() {
			@Override
			public void onRefresh() {
				pageNumber = 1;
				new TransfersListTask(pageNumber, pageSize, false, "transfersList", "2").execute();
			}

			@Override
			public void onLoadMore() {
				pageNumber++;
				new TransfersListTask(pageNumber, pageSize, true, "transfersList", "2").execute();
			}
		});

		// 已转让
		View viewtransferablealready = mInflater.inflate(R.layout.transferablealready_lisstview, null);
		mListView.add(viewtransferablealready);
		mXvTransferablealready = (XListView) viewtransferablealready.findViewById(R.id.xlvTransferablealready);
		mCaTransferablealready = new CommonAdapter(mContext, mListtransferablealready,
				R.layout.item_transferablealerady, ViewHolderTransferablealready.class,
				handleCallBackTransferablealready);
		mXvTransferablealready.setAdapter(mCaTransferablealready);

		mXvTransferablealready.setPullLoadEnable(true);
		mXvTransferablealready.setPullRefreshEnable(true);
		mXvTransferablealready.mFooterView.hide(); // 隐藏更多
		mXvTransferablealready.setRefreshTime(sdf.format(new Date()));
		mXvTransferablealready.setXListViewListener(new IXListViewListener() {
			@Override
			public void onRefresh() {
				pageNumber = 1;
				new TransfersListTask(pageNumber, pageSize, false, "transfersList", "1").execute();
			}

			@Override
			public void onLoadMore() {
				pageNumber++;
				new TransfersListTask(pageNumber, pageSize, true, "transfersList", "1").execute();
			}
		});

		// 已购买
		View alreadybuy = mInflater.inflate(R.layout.already_buy_lisstview, null);
		mListView.add(alreadybuy);
		mXvAlreadyBuy = (XListView) alreadybuy.findViewById(R.id.xlvAlreadybuy);
		mCaAlreadyBuy = new CommonAdapter(mContext, mListAleradyBuy, R.layout.item_alerady_buy,
				ViewHolderAleradyBuy.class, handleCallBackAleradyBuy);
		mXvAlreadyBuy.setAdapter(mCaAlreadyBuy);

		mXvAlreadyBuy.setPullLoadEnable(true);
		mXvAlreadyBuy.setPullRefreshEnable(true);
		mXvAlreadyBuy.mFooterView.hide(); // 隐藏更多
		mXvAlreadyBuy.setRefreshTime(sdf.format(new Date()));
		mXvAlreadyBuy.setXListViewListener(new IXListViewListener() {
			@Override
			public void onRefresh() {
				pageNumber = 1;
				new TransfersListTask(pageNumber, pageSize, false, "transfersList", "4").execute();
			}

			@Override
			public void onLoadMore() {
				pageNumber++;
				new TransfersListTask(pageNumber, pageSize, true, "transfersList", "4").execute();
			}
		});
		mVpDebtor.setAdapter(new MyPagerAdapter(mListView));
		mVpDebtor.setCurrentItem(0);
		mVpDebtor.setOnPageChangeListener(new MyOnPageChangeListener());

	}

	/**
	 * 初始化动画
	 */
	private void InitImageView() {
		mBmpW = BitmapFactory.decodeResource(getResources(), R.drawable.delimiter_invest_list).getWidth();// 获取图片宽度
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenW = dm.widthPixels;// 获取分辨率宽度
		mOffset = (screenW / 4);// 计算偏移量
		RelativeLayout.LayoutParams mLayoutParams = (RelativeLayout.LayoutParams) mIvCursorDebtor.getLayoutParams();
		mLayoutParams.width = mOffset;
		mIvCursorDebtor.setLayoutParams(mLayoutParams);
		Matrix matrix = new Matrix();
		matrix.postTranslate(mOffset, 0);
		mIvCursorDebtor.setImageMatrix(matrix);// 设置动画初始位置
	}

	/**
	 * ViewPager适配器
	 */
	public class MyPagerAdapter extends PagerAdapter {
		public List<View> mListViews;

		public MyPagerAdapter(List<View> mListViews) {
			this.mListViews = mListViews;
		}

		@Override
		public void destroyItem(View arg0, int arg1, Object arg2) {
			((ViewPager) arg0).removeView(mListViews.get(arg1));
		}

		@Override
		public void finishUpdate(View arg0) {
		}

		@Override
		public int getCount() {
			return mListViews.size();
		}

		@Override
		public Object instantiateItem(View arg0, int arg1) {
			((ViewPager) arg0).addView(mListViews.get(arg1), 0);
			return mListViews.get(arg1);
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == (arg1);
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {
		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void startUpdate(View arg0) {
		}
	}

	/**
	 * 页卡切换监听
	 */
	public class MyOnPageChangeListener implements OnPageChangeListener {

		@Override
		public void onPageSelected(int arg0) {

			Animation animation = null;
			animation = new TranslateAnimation(mOffset * currIndex, mOffset * arg0, 0, 0);
			currIndex = arg0;
			animation.setFillAfter(true);// True：图片停在动画结束位置
			animation.setDuration(300);
			mIvCursorDebtor.startAnimation(animation);
			mTabCurrentPosition = arg0;
			setCurrentPosition(arg0);
			if (arg0 == 1) {// 转让中
				if (mListTransfering.size() == 0) {
					showProgressDialog();
					new TransfersListTask(pageNumber, pageSize, false, "transfersList", "2").execute();
				}
			} else if (arg0 == 2) {// 已转让
				if (mListtransferablealready.size() == 0) {
					showProgressDialog();
					new TransfersListTask(pageNumber, pageSize, false, "transfersList", "1").execute();
				}
			} else if (arg0 == 3) {// 已购买
				if (mListAleradyBuy.size() == 0) {
					showProgressDialog();
					new TransfersListTask(pageNumber, pageSize, false, "transfersList", "4").execute();
				}
			}
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
		}
	}

	/****** 网络请求数据 *********/
	private class TransfersListTask extends AsyncTask<Void, Void, String> {
		// CanTransListDataClass 可转让列表
		private CanTransListDataClass dc = new CanTransListDataClass();
		private TransferDebtDataClass dcT = new TransferDebtDataClass();
		RequestObject mObject = new RequestObject();
		private int pageNumber;
		private int pageSize;
		private boolean isAdd;
		private String requestMethod;
		private String status;

		public TransfersListTask(int pageNumber, int pageSize, boolean isAdd, String requestMethod, String status) {
			this.pageNumber = pageNumber;
			this.pageSize = pageSize;
			this.isAdd = isAdd;
			this.requestMethod = requestMethod;
			this.status = status;
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
		}

		@Override
		protected String doInBackground(Void... params) {
			// TODO Auto-generated method stub
			if (requestMethod.equals(REQUESTMETHOD)) {
				mObject.method = "canTransList";
				mObject.map.put("pageNumber", pageNumber);
				mObject.map.put("pageSize", pageSize);
				return requestDataAndGetErrorMsg(mObject, dc);
			} else {
				mObject.method = "transfersList";
				mObject.map.put("pageNumber", pageNumber);
				mObject.map.put("pageSize", pageSize);
				mObject.map.put("status", status);
				return requestDataAndGetErrorMsg(mObject, dcT);
			}
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			if ("-1".equals(dc.code) || "-1".equals(dcT.code)) {
				statusRefresh = status;
				loginWebview();
				return;
			}
			if (TextUtils.isEmpty(result)) {// success

				if (requestMethod.equals(REQUESTMETHOD)) {
					if (!isAdd) {
						mListTransferaable.clear();
					}
					if (dc.list != null && dc.list.size() == 0) {
						showToast("暂无信息");
					}
					mListTransferaable.addAll(dc.list);
					if (mListTransferaable.size() <= (dc.totalPage - 1) * pageSize) {
						mXvTransferable.mFooterView.show();
					} else {
						mXvTransferable.mFooterView.hide();
					}

					mCaTransferable.notifyDataSetChanged();
				} else {
					if (status.equals("1")) {
						if (!isAdd) {
							mListtransferablealready.clear();
						}
						if (dcT.list != null && dcT.list.size() == 0) {
							showToast("暂无信息");
						}
						mListtransferablealready.addAll(dcT.list);
						if (mListtransferablealready.size() <= (dc.totalPage - 1) * pageSize) {
							mXvTransferablealready.mFooterView.show();
						} else {
							mXvTransferablealready.mFooterView.hide();
						}

						mCaTransferablealready.notifyDataSetChanged();
					} else if (status.equals("2")) {
						if (!isAdd) {
							mListTransfering.clear();
						}
						statusRevoke = dcT.handCancel;
						if (dcT.list != null && dcT.list.size() == 0) {
							showToast("暂无信息");
						}
						mListTransfering.addAll(dcT.list);
						if (mListTransfering.size() <= (dc.totalPage - 1) * pageSize) {
							mXvTransferring.mFooterView.show();
						} else {
							mXvTransferring.mFooterView.hide();
						}

						mCaTransferring.notifyDataSetChanged();
					} else if (status.equals("4")) {
						if (!isAdd) {
							mListAleradyBuy.clear();
						}
						if (dcT.list != null && dcT.list.size() == 0) {
							showToast("暂无信息");
						}
						mListAleradyBuy.addAll(dcT.list);
						if (mListAleradyBuy.size() <= (dc.totalPage - 1) * pageSize) {
							mXvAlreadyBuy.mFooterView.show();
						} else {
							mXvAlreadyBuy.mFooterView.hide();
						}

						mCaAlreadyBuy.notifyDataSetChanged();
					}

				}

			} else {// fail
				showToast(result);
			}
			dismissProgressDialog();
			mXvTransferable.stopLoadMore();
			mXvTransferable.stopRefresh();
			mXvAlreadyBuy.stopLoadMore();
			mXvAlreadyBuy.stopRefresh();
			mXvTransferring.stopLoadMore();
			mXvTransferring.stopRefresh();
			mXvTransferablealready.stopLoadMore();
			mXvTransferablealready.stopRefresh();
		}
	}

	/********* onActivityResult ********/
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, intent);
		if (resultCode == RESULT_OK) {
			switch (requestCode) {
			case CommonData.REQUEST_CODE_LOGIN:
				if (!TextUtils.isEmpty(statusRefresh)) {
					pageNumber = 1;
					new TransfersListTask(pageNumber, pageSize, false, "transfersList", statusRefresh).execute();
				} else {
					pageNumber = 1;
					new TransfersListTask(pageNumber, pageSize, false, REQUESTMETHOD, "").execute();
				}
				break;
			case CommonData.CONFIRMATION_TRANSFER:
				// 够买成功返回刷新
				pageNumber = 1;
				showProgressDialog();
				new TransfersListTask(pageNumber, pageSize, false, REQUESTMETHOD, "").execute();
				break;
			default:
				break;
			}
		}
	}

	private void setCurrentPosition(int position) {
		if (position == 0) {
			mTvTransferable.setSelected(true);// 可转让
			mTvTransferring.setSelected(false);// 转让中
			mtvTransferablealready.setSelected(false);// 已转让
			mTvAlreadyBuy.setSelected(false);// 已购买
		} else if (position == 1) {
			mTvTransferable.setSelected(false);// 可转让
			mTvTransferring.setSelected(true);// 转让中
			mtvTransferablealready.setSelected(false);// 已转让
			mTvAlreadyBuy.setSelected(false);// 已购买
		} else if (position == 2) {
			mTvTransferable.setSelected(false);// 可转让
			mTvTransferring.setSelected(false);// 转让中
			mtvTransferablealready.setSelected(true);// 已转让
			mTvAlreadyBuy.setSelected(false);// 已购买
		} else if (position == 3) {
			mTvTransferable.setSelected(false);// 可转让
			mTvTransferring.setSelected(false);// 转让中
			mtvTransferablealready.setSelected(false);// 已转让
			mTvAlreadyBuy.setSelected(true);// 已购买
		}
	}

	public static class ViewHolderTransferablealready {
		TextView tvTransferablealeradytradeName;// 交易名称
		TextView tvTransferablealeradyamount;// 转让价格
		TextView tvTransferablealeradyrealProfitRate;// 预期年化利率
		TextView tvTransferablealeradyprofitRate;// 原年化利率
		TextView tvTransferablealeradyoriginalAmount;// 投资金额
		TextView tvTransferablealeradytradeExpireDate;// 到期时间
		TextView tvTransferablealeradytransferExpireDate;// 转让成交时间
		TextView tvTransferablealeradychargeAmount;// 转让手续费
		TextView tvTransferablealeradyleftAllTime;// 剩余/总期限
	}

	// 已转让
	private HandleCallBack handleCallBackTransferablealready = new HandleCallBack() {

		@Override
		public void handle(int position, View convertView, ViewGroup parent, Object holder, Object item) {
			// TODO Auto-generated method stub
			ViewHolderTransferablealready viewHolderTransferablealready = (ViewHolderTransferablealready) holder;
			TransfersListInfo info = (TransfersListInfo) item;
			viewHolderTransferablealready.tvTransferablealeradytradeName.setText(info.tradeName);// 交易名称
			viewHolderTransferablealready.tvTransferablealeradyamount.setText("￥" + AppUtil.changeMoneyCompany(info.amount));// 转让价格
			String strRealProfitRate = "";
			if (!TextUtils.isEmpty(info.realProfitRate)) {

				strRealProfitRate = df.format((Double.parseDouble(info.realProfitRate) * 100)) + "%";
			}
			viewHolderTransferablealready.tvTransferablealeradyrealProfitRate.setText(strRealProfitRate);// 预期年化利率
			String strProfitRate = "";
			if (!TextUtils.isEmpty(info.profitRate)) {

				strProfitRate = df.format((Double.parseDouble(info.profitRate) * 100)) + "%";
			}
			viewHolderTransferablealready.tvTransferablealeradyprofitRate.setText(strProfitRate);// 原年化利率
			viewHolderTransferablealready.tvTransferablealeradyoriginalAmount.setText("￥"
					+ AppUtil.changeMoneyCompany(info.originalAmount));// 投资金额
			if (!TextUtils.isEmpty(info.tradeExpireDate) && info.tradeExpireDate.length() > 10) {
				viewHolderTransferablealready.tvTransferablealeradytradeExpireDate.setText(info.tradeExpireDate
						.substring(0, 10));// 到期时间
			} else {
				viewHolderTransferablealready.tvTransferablealeradytradeExpireDate.setText(info.tradeExpireDate);// 到期时间
			}
			if (!TextUtils.isEmpty(info.transferDate) && info.transferDate.length() > 10) {
				viewHolderTransferablealready.tvTransferablealeradytransferExpireDate.setText(info.transferDate
						.substring(0, 10));// 转让成交时间

			} else {
				viewHolderTransferablealready.tvTransferablealeradytransferExpireDate.setText(info.transferDate);// 转让成交时间
			}

			viewHolderTransferablealready.tvTransferablealeradychargeAmount.setText("￥"
					+ AppUtil.changeMoneyCompany(info.chargeAmount));// 转让手续费
			viewHolderTransferablealready.tvTransferablealeradyleftAllTime.setText(info.leftTime.replace("天", "") + "/"
					+ info.allTime + "天");// 剩余/总期限
		}
	};

	public static class ViewHolderAleradyBuy {
		TextView tvAleradyBuytradeName;// 交易名称
		TextView tvAleradyBuyInterestAmount;// 收益
		TextView tvAleradyBuyprofitRate;// 年化利率
		TextView tvAleradyBuytransferDate;// 购买时间
		TextView tvAleradyBuyamount;// 购买价格
		TextView tvAleradyBuytradeExpireDate;// 到期时间
		TextView tvAleradyBuystatus;// 状态
	}

	// 已购买
	private HandleCallBack handleCallBackAleradyBuy = new HandleCallBack() {

		@Override
		public void handle(int position, View convertView, ViewGroup parent, Object holder, Object item) {
			// TODO Auto-generated method stub
			TransfersListInfo transfersListInfo = (TransfersListInfo) item;
			ViewHolderAleradyBuy viewHolderAleradyBuy = (ViewHolderAleradyBuy) holder;
			viewHolderAleradyBuy.tvAleradyBuytradeName.setText(transfersListInfo.tradeName);// 交易名称
			viewHolderAleradyBuy.tvAleradyBuyInterestAmount.setText("￥"
					+ AppUtil.changeMoneyCompany(transfersListInfo.interestAmount));// 收益
			String strprofitRate = "";
			if (!TextUtils.isEmpty(transfersListInfo.realProfitRate)) {
				strprofitRate = df.format((Double.parseDouble(transfersListInfo.realProfitRate) * 100)) + "%";
			}
			viewHolderAleradyBuy.tvAleradyBuyprofitRate.setText(strprofitRate);// 年化利率
			if (!TextUtils.isEmpty(transfersListInfo.transferDate) && transfersListInfo.transferDate.length() > 10) {
				viewHolderAleradyBuy.tvAleradyBuytransferDate.setText(transfersListInfo.transferDate.substring(0, 10));// 购买时间
			} else if (!TextUtils.isEmpty(transfersListInfo.transferDate)) {
				viewHolderAleradyBuy.tvAleradyBuytransferDate.setText(transfersListInfo.transferDate);// 购买时间
			}
			viewHolderAleradyBuy.tvAleradyBuyamount.setText("￥" + AppUtil.changeMoneyCompany(transfersListInfo.amount));// 购买价格
			if (!TextUtils.isEmpty(transfersListInfo.tradeExpireDate) && transfersListInfo.tradeExpireDate.length() > 10) {
				viewHolderAleradyBuy.tvAleradyBuytradeExpireDate.setText(transfersListInfo.tradeExpireDate.substring(0,
						10));// 到期时间
			} else if (!TextUtils.isEmpty(transfersListInfo.tradeExpireDate)) {
				viewHolderAleradyBuy.tvAleradyBuytradeExpireDate.setText(transfersListInfo.tradeExpireDate);// 到期时间
			}
			String tradeStatus = "无";
			if (!TextUtils.isEmpty(transfersListInfo.status)) {

				if ("1".equals(transfersListInfo.tradeStatus)) {
					tradeStatus = "正在投资";

				} else if ("2".equals(transfersListInfo.tradeStatus)) {
					tradeStatus = "投资已满";
				} else if ("3".equals(transfersListInfo.tradeStatus)) {
					tradeStatus = "未到投资时间";
				} else if ("4".equals(transfersListInfo.tradeStatus)) {
					tradeStatus = "还款完成";
				} else if ("5".equals(transfersListInfo.tradeStatus)) {
					tradeStatus = "正在还款";
				} else if ("6".equals(transfersListInfo.tradeStatus)) {
					tradeStatus = "已过投资时间";
				} else if ("7".equals(transfersListInfo.tradeStatus)) {
					tradeStatus = "正在还款";
				} else if ("8".equals(transfersListInfo.tradeStatus)) {
					tradeStatus = "交易失败";
				}

			}
			viewHolderAleradyBuy.tvAleradyBuystatus.setText(tradeStatus);// 状态
		}
	};

	public static class ViewHolderTransfering {
		LinearLayout llTransferingstatu;
		TextView tvTransferringtradeName;// 交易名称
		TextView tvTransferringamount;// 转让价格
		TextView tvTransferringoriginalAmount;// 投资金额
		TextView tvTransferringchargeAmount;// 转让手续费
		TextView tvTransferringtransferExpireDate;// 转让截止时间
		TextView tvTransferringtradeExpireDate;// 到期时间
		TextView tvTransferringstatus;// 撤销状态
		TextView tvTransferringleftAllTime;// 剩余/总期限
		TextView tvTransferringprofitRate;// 预期年利化率

	}

	// 转让中
	private HandleCallBack handleCallBackTransfering = new HandleCallBack() {

		@Override
		public void handle(final int position, View convertView, ViewGroup parent, Object holder, Object item) {
			// TODO Auto-generated method stub
			ViewHolderTransfering viewHolderTransfering = (ViewHolderTransfering) holder;
			TransfersListInfo transfersListInfo = (TransfersListInfo) item;
			viewHolderTransfering.tvTransferringtradeName.setText(transfersListInfo.tradeName);// 交易名称
			viewHolderTransfering.tvTransferringamount.setText("￥" + AppUtil.changeMoneyCompany(transfersListInfo.amount));// 转让价格
			viewHolderTransfering.tvTransferringoriginalAmount.setText("￥"
					+ AppUtil.changeMoneyCompany(transfersListInfo.originalAmount));// 投资金额
			viewHolderTransfering.tvTransferringchargeAmount.setText("￥"
					+ AppUtil.changeMoneyCompany(transfersListInfo.chargeAmount));// 转让手续费
			if (!TextUtils.isEmpty(transfersListInfo.transferExpireDate) && transfersListInfo.transferExpireDate.length() > 10) {
				viewHolderTransfering.tvTransferringtransferExpireDate.setText(transfersListInfo.transferExpireDate
						.substring(0, 10));// 转让截止时间
			} else if (!TextUtils.isEmpty(transfersListInfo.transferExpireDate)) {
				viewHolderTransfering.tvTransferringtransferExpireDate.setText(transfersListInfo.transferExpireDate);// 转让截止时间
			}
			if (!TextUtils.isEmpty(transfersListInfo.tradeExpireDate) && transfersListInfo.tradeExpireDate.length() > 10) {
				viewHolderTransfering.tvTransferringtradeExpireDate.setText(transfersListInfo.tradeExpireDate
						.substring(0, 10));// 到期时间
			} else if (!TextUtils.isEmpty(transfersListInfo.tradeExpireDate)){
				viewHolderTransfering.tvTransferringtradeExpireDate.setText(transfersListInfo.tradeExpireDate);// 到期时间
			}
			if (!TextUtils.isEmpty(transfersListInfo.realProfitRate) && -1 != (transfersListInfo.realProfitRate.indexOf("-"))) {
				viewHolderTransfering.tvTransferringstatus.setText("转让失败"); // 撤销状态
				viewHolderTransfering.llTransferingstatu.setEnabled(false); // 撤销状态
			} else {
				viewHolderTransfering.tvTransferringstatus.setText("撤销转让"); // 撤销状态
				viewHolderTransfering.llTransferingstatu.setEnabled(true); // 撤销状态
			}
			
			String leftTimeStr = transfersListInfo.leftTime;
			if(!TextUtils.isEmpty(leftTimeStr)) {
				leftTimeStr = leftTimeStr.replace("天", "");
			} else {
				leftTimeStr = "0";
			}
			String allTimeStr = transfersListInfo.allTime;
			if(TextUtils.isEmpty(allTimeStr)) {
				allTimeStr = "0";
			}
			
			viewHolderTransfering.tvTransferringleftAllTime.setText("剩余/总期限        "
					+ leftTimeStr/*transfersListInfo.leftTime.replace("天", "")*/ + "/" + allTimeStr/*transfersListInfo.allTime*/ + "天");// 剩余/总期限
			if (!TextUtils.isEmpty(transfersListInfo.realProfitRate)) {
				String strprofitRate;
				strprofitRate = df.format((Double.parseDouble(transfersListInfo.realProfitRate) * 100));
				viewHolderTransfering.tvTransferringprofitRate.setText(strprofitRate + "%");// 预期年利化率
			}
			if (!TextUtils.isEmpty(statusRevoke) && "IS".equals(statusRevoke)) {
				viewHolderTransfering.llTransferingstatu.setEnabled(true);
				viewHolderTransfering.llTransferingstatu.setBackgroundResource(R.drawable.shape_transfer);// btn_selector
			} else {
				viewHolderTransfering.llTransferingstatu.setEnabled(false);
				viewHolderTransfering.llTransferingstatu.setBackgroundResource(R.drawable.shape_btn_bg_revoke_pressed);
			}
			tradeIdDl = transfersListInfo.tradeId;
			tradeTransferIdDl = transfersListInfo.tradeTransferId;
			viewHolderTransfering.llTransferingstatu.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub

					mDialogTransfering.show();
				}
			});
		}
	};

	public static class ViewHolderTransferable {
		TextView tvtrfLoanName;// 项目名称
		TextView tvTrfinvestAmount;// 投资金额
		TextView tvTrfleftTime;// 剩余时间
		TextView tvTrfinterestRate;// 年利化率
		TextView tvTrfinvestDate;// 投资时间
		TextView tvTrffinishDate;// 到期時間
		TextView tvLeftTotalTime;// 投资剩余时间
	}

	// 可转让
	private HandleCallBack handleCallBackTransferable = new HandleCallBack() {

		@Override
		public void handle(final int position, View convertView, ViewGroup parent, Object holder, Object item) {
			// TODO Auto-generated method stub
			ViewHolderTransferable viewHolder = (ViewHolderTransferable) holder;
			final CanTransListInfo clInfo = (CanTransListInfo) item;
			viewHolder.tvtrfLoanName.setText(clInfo.loanName);// 项目名称
			viewHolder.tvTrfinvestAmount.setText("￥" + AppUtil.changeMoneyCompany(clInfo.investAmount));// 投资金额
			viewHolder.tvTrfleftTime.setText(clInfo.leftTime);// 剩余时间
			viewHolder.tvTrfinterestRate.setText(clInfo.interestRate);// 年利化率
			investAmount = clInfo.investAmount;
			if (!TextUtils.isEmpty(clInfo.investDate) && clInfo.investDate.length() > 10) {
				viewHolder.tvTrfinvestDate.setText(clInfo.investDate.substring(0, 10));// 投资时间
			} else if(!TextUtils.isEmpty(clInfo.investDate)) {
				viewHolder.tvTrfinvestDate.setText(clInfo.investDate);// 投资时间
			}
			if (!TextUtils.isEmpty(clInfo.finishDate) && clInfo.finishDate.length() > 10) {
				viewHolder.tvTrffinishDate.setText(clInfo.finishDate.substring(0, 10));// 到期時間
			} else if (!TextUtils.isEmpty(clInfo.finishDate)) {
				viewHolder.tvTrffinishDate.setText(clInfo.finishDate);// 到期時間
			}
			viewHolder.tvLeftTotalTime.setText("剩余/总期限        " + clInfo.leftTime + "/" + clInfo.allTime + "天");// 投资剩余时间
			convertView.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent intent = new Intent(MyDebtorActivity.this, ApplicationTransferActivity.class);
					String aaa = clInfo.tradeId;
					intent.putExtra("tradeId", clInfo.tradeId);
					intent.putExtra("investAmount", investAmount);
					intent.putExtra("tradeDetailId", clInfo.tradeDetailId);
					startActivityForResult(intent, CommonData.CONFIRMATION_TRANSFER);

				}
			});
		}
	};

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.tvTransferable:// 可转让
			mVpDebtor.setCurrentItem(0);
			break;
		case R.id.tvTransferring:// 转让中
			mVpDebtor.setCurrentItem(1);
			break;
		case R.id.tvTransferablealready:// 已转让
			mVpDebtor.setCurrentItem(2);
			break;
		case R.id.tvAlreadyBuy:// 已购买
			mVpDebtor.setCurrentItem(3);
			break;
		case R.id.btDialogtrasferCancel:
			mDialogTransfering.dismiss();
			break;
		case R.id.btDialogtrasferOk:
			mDialogTransfering.dismiss();
			showProgressDialog();
			new CancelTransferTask(tradeIdDl, tradeTransferIdDl).execute();
			break;
		case R.id.tv_title_right:
			startActivity(new Intent(mContext, BondsPurchasedActivity.class));
			break;
		default:
			break;
		}
	}

	/**************** 取消转让申请 *************/
	public class CancelTransferTask extends AsyncTask<Void, Void, String> {
		RequestObject mObject = new RequestObject();
		private String tradeId;// 查询Id
		private String tradeTransferId;// 查询Id
		CancelTransferDataClass dc = new CancelTransferDataClass();

		public CancelTransferTask(String tradeId, String tradeTransferId) {
			this.tradeId = tradeId;
			this.tradeTransferId = tradeTransferId;
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
		}

		@Override
		protected String doInBackground(Void... params) {
			// TODO Auto-generated method stub
			mObject.method = "cancelTransfer";
			mObject.map.put("tradeId", tradeId);
			mObject.map.put("tradeTransferId", tradeTransferId);
			return requestDataAndGetErrorMsg(mObject, dc);
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			dismissProgressDialog();
			if ("-1".equals(dc.code)) {
				loginWebview();
				return;
			}
			if (TextUtils.isEmpty(result)) {// success
				showToast(dc.msg);
				pageNumber = 1;
				new TransfersListTask(pageNumber, pageSize, false, "transfersList", "2").execute();
			} else {// fail
				showToast(result);
			}
		}
	}

}
